// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <melon/random.h>
#include <melon/init/init.h>
#include <melon/benchmark.h>
#include <pollux/type/type.h>

namespace kumo::pollux {
namespace {

void runStringViewCreate(uint32_t iterations, uint32_t len) {
  melon::BenchmarkSuspender suspender;

  const std::string chars =
      "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  if (len > chars.size()) {
    len = chars.size();
  }
  size_t sum = 0;
  suspender.dismiss();

  for (auto i = 0; i < iterations; i++) {
    StringView str(chars.data() + i % (chars.size() - len), len);
    sum += str.data()[0];
  }

  melon::doNotOptimizeAway(sum);
}

constexpr auto INLINE_SIZE = StringView::kInlineSize;
constexpr auto NON_INLINE_SIZE = StringView::kInlineSize + 10;

// Short strings which can be inlined.
BENCHMARK_PARAM(runStringViewCreate, INLINE_SIZE);

// Larger strings which won't be inlined.
BENCHMARK_PARAM(runStringViewCreate, NON_INLINE_SIZE);
} // namespace
} // namespace kumo::pollux

int main(int argc, char** argv) {
  melon::Init init{&argc, &argv};
  melon::runBenchmarks();
  return 0;
}
